package vk
{
  import Tools.Debug;

  /**
   * ...
   * @author Roman Marchenko
   */
  public class Api extends APIConnection
  {
    private static var appvars:Object; // stage.loaderInfo.parameters as Object;
    private static var testMode:uint;

    public function Api()
    {
      CONFIG::debug
      {
        if (null == appvars)
        {
          Debug.log("vk.Api: WARNING: The app vars are not initialized. Call setUp() method.");
        }
      }
      super(appvars);
    }

    public static function setUp(params:Object):void
    {
      testMode = 0;
      appvars = params;

      if (null == appvars['api_id'])
      {
        Debug.log("vk.Api: local testing mode");
        // -- Local testing settings
        // Note that the local testing settings are not under
        // source control system due to security issues.
        appvars['api_id'] = LocalTesting.API_ID;
        appvars['viewer_id'] = LocalTesting.VIEWER_ID;
        appvars['sid'] = LocalTesting.SID;
        appvars['secret'] = LocalTesting.SECRET;
        // -- //
        testMode = 1;
      }
    }

    override public function api(method:String, params:Object, onComplete:Function = null, onError:Function = null):void
    {
      if (1 == testMode)
      {
        params["test_mode"] = 1;
      }

      CONFIG::debug
      {
        Debug.log("vk.Api.execute: " + method);
        for (var i:String in params)
        {
          Debug.log("vk.Api.execute:   - " + i + " : " + params[i]);
        }
      }

      super.api(method, params, onComplete, onError);
    }

  }

}